package com.guomao;

import com.entity.User;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/msc")
public class MyServletC extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("application/json;charset=utf-8");

        StringBuilder stringBuilder = new StringBuilder();

        ////reader是用来读取请求体数据 post
        BufferedReader reader = req.getReader();

        String line;

        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
        }

//        System.out.println();

        String msg = stringBuilder.toString();
        System.out.println(line + ">>>>>>>>>>>>>>>>>>>" + msg);


//        {"name":"zhangsan","age":80,"email":"zhangsan@gmail.com"}
        Gson gson = new Gson();

        User user = gson.fromJson(msg, User.class);

        user.setAge(user.getAge() - 40);
//        System.out.println(user);

        String str = gson.toJson(user);

        resp.getWriter().write(str);

    }
}
